﻿#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using AbstractClass;
using AbstractClass.Extensions;
using AbstractClass.Linq;
using NUnit.Framework;

namespace QuasarTest.Core
{
    [TestFixture]
    public class PipeTest
    {
        #region Setup/Teardown

        [SetUp]
        public void Init()
        {
            _pipes = Pipe<int>.Create(Add, Square);
            _data = Iterable.Generate(1, 2, 10);
        }

        #endregion

        private Pipe<int> _pipes;
        private IEnumerable<int> _data;

        private int Add(int j)
        {
            return j + j;
        }

        private int Square(int j)
        {
            return j * j;
        }

        [Test]
        public void TestPipeline()
        {
            IEnumerable<int> result = _data.Pipeline(_pipes);
            Assert.AreEqual(result.First(), (1 + 1) * (1 + 1));
            Assert.AreEqual(result.Count(), 10);
            Assert.AreEqual(result.Last(), (19 + 19) * (19 + 19));
            Console.WriteLine("Input date");
            _data.Process(Console.WriteLine);
            Console.WriteLine("Ouput data");
            result.Process(Console.WriteLine);

            _pipes.AddPipe(Add).AddPipe(Square);
            result = _data.Pipeline(_pipes);
            Assert.AreEqual(result.First(), 64);

            Pipe<int> pipe = new Func<int, int>(Add).CreatePipe().AddPipe(Square, Square);
            result = _data.Pipeline(pipe);
            Assert.AreEqual(result.First(), 16);
            Assert.AreEqual(result.Count(), 10);
        }
    }
}